package com.tjsiqi.controller;

import com.tjsiqi.beans.PageQuery;
import com.tjsiqi.common.JsonData;
import com.tjsiqi.model.GybWeighRecordEXP;
import com.tjsiqi.param.WeighCountParam;
import com.tjsiqi.service.SysWeighOrderService;
import com.tjsiqi.util.DateTimeUtil;
import com.tjsiqi.util.ExportExcelUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

/**
 * Created by lxc
 */

@Controller
@RequestMapping("/sys/WeighOrder")
public class SysWeighOrderController {

    @Resource
    private SysWeighOrderService sysWeighOrderService;
//  跳转进货单页面
    @RequestMapping("/WeighOrder.page")
    public ModelAndView WeighOrderPage() {
        return new ModelAndView("WeighOrder");
    }

//  查询进货单
    @RequestMapping("/page.json")
    @ResponseBody
    public JsonData searchPage(WeighCountParam WeighCountParam, PageQuery page) {
        return JsonData.success(sysWeighOrderService.searchPageList(WeighCountParam, page));
    }
//  导出进货单
    @RequestMapping("/export")
    @ResponseBody
    public JsonData exportByParam(HttpServletRequest request, HttpServletResponse response, WeighCountParam WeighCountParam) throws IOException {
        String filename = "进货单" + DateTimeUtil.dateToStr(new Date(), "yyyy-MM-dd");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8") + ".xls");
        response.setContentType("application/x-download");
        ExportExcelUtil<GybWeighRecordEXP> util = new ExportExcelUtil<GybWeighRecordEXP>();
        List<GybWeighRecordEXP> WeighOrderList = sysWeighOrderService.exportByParam(WeighCountParam);
        OutputStream ouputStream = response.getOutputStream();
        String[] columnNames = {"编号", "计划编号", "车号", "过磅类别", "货物名称", "货物规格", "皮重", "毛重", "净重", "另扣-比例", "另扣", "结算", "单价", "金额", "供货单位", "收货单位", "货运单位", "过皮时间", "过毛时间", "存放位置", "创建时间", "司磅员", "备注", "状态"};
        util.exportExcel("进货单列表", columnNames, WeighOrderList, ouputStream, ExportExcelUtil.EXCEL_FILE_2003);
        return JsonData.success();
    }
//  删除所选搅拌站
    @RequestMapping("/remove")
    @ResponseBody
    public JsonData removeByParam(HttpServletRequest request, HttpServletResponse response,WeighCountParam weighCountParam) throws IOException {
        if (StringUtils.isBlank(weighCountParam.getJbzCode())) {
            return JsonData.fail("搅拌站编号获取失败，请刷新重试！");
        } else{
            int isRemove = sysWeighOrderService.removeByParam(weighCountParam);
            System.out.println("删除所选搅拌站"+isRemove);
            return JsonData.success(isRemove);
        }
    }


}
